package com.boot;

import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        Socket s=null;
        //图片所在文件夹
        String path="src/main/resources/static";
        try {
            //对方的端口号和ip
            s = new Socket("10.16.39.52", 10086);
            File file = new File(path);
            File[] files = file.listFiles();
            byte[] bytes;
            BufferedOutputStream bo = new BufferedOutputStream(s.getOutputStream());
            if (null!=files){
                //将要传输的图片数量装在第一个字节发过去
                bo.write((byte) files.length);
                bo.flush();
                for (File value : files) {
                    BufferedInputStream bi = new BufferedInputStream(new FileInputStream(value));
                    //图片有多大就直接发多大，如果报错就一次少发点
                    bytes = new byte[(int) value.length()];
                    while (bi.read(bytes) != -1) {
                        bo.write(bytes);
                        bo.flush();
                    }//每个文件传输完成的标志
                    bo.write("ReceiveOver".getBytes());
                    bo.flush();
                    bi.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (s!=null) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
